/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 31.07.2005
 *
 *************************************************/
package fightplan.views.tables;

import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;

import fightplan.business.Gender;
import fightplan.ui.BeanPropertyListCellRenderer;

@SuppressWarnings("serial")
public class GenderCellEditor extends DefaultCellEditor {

	public GenderCellEditor(Gender[] possibilities) {
		super(createComboBox(possibilities));
	}

	private static JComboBox createComboBox(Gender[] possibilities) {
		JComboBox comboBox = new JComboBox(possibilities);
		comboBox.setRenderer(new BeanPropertyListCellRenderer("nameLong"));
		comboBox.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		comboBox.setEditable(false);
		return comboBox;
	}

	public Object getCellEditorValue() {
		Object o = ((JComboBox) getComponent()).getSelectedItem();
		if (o instanceof Gender) {
			return o;
		} else if (o instanceof String) {
			String s = (String) o;
			for (int i = 0; i < Gender.ALL_VALUES.length; i++) {
				if (Gender.ALL_VALUES[i].getName().equalsIgnoreCase(s)
						|| Gender.ALL_VALUES[i].getNameLong().equalsIgnoreCase(s)) {
					return Gender.ALL_VALUES[i];
				}
			}
		}
		return Gender.UNSET;
	}

}
